package com.wutong.erb.dto;

import com.wutong.erb.entity.user.User;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 用户DTO，用于接收前端传来的用户信息，是用户模块的总入口
 * @author wutong
 */
public class UserDTO {

    private static final String USERNAME_REGEXP = "^(?![0-9]+$)[A-Za-z][A-Za-z0-9_]{3,19}$";
    /**
     * 密码正则表达式，位数为8-20位，需要包含大小写字母和数字 特殊字符（可选）
     */
    private static final String PWD_REGEXP = "^(?=.*[a-zA-Z])(?=.*\\d)[\\w!@#$%^&*()-+=]{8,20}$";
    private static final String PHONE_REGEXP = "^1[3-9]\\d{9}$";

    /**
     * 用户信息DTO
     * @author wutong
     */
    @Data
    public static class UserInfoDTO {
        private Long id;
        private String username;
        private User.Sex sex;
        private String phone;
        private String email;
        private User.UserType userType;
        private String wechatOpenid;
        private String qqOpenid;
        private String avatarUrl;
        private String realName;
        private String nickname;
        private User.UserStatus status;
    }

    /**
     * 用户登录DTO
     * @author wutong
     */
    @Data
    public static class UserLoginDTO {
        @Size(min = 4, max = 20, message = "用户名长度4-20位")
        @Pattern(
                regexp = USERNAME_REGEXP,
                message = "需以字母开头，可包含数字和下划线，长度4-20位，不能全为数字"
        )
        private String username;
        @Email(message = "邮箱格式不正确")
        private String email;
        @NotBlank(message = "密码不能为空")
        @Pattern(
                regexp = PWD_REGEXP,
                message = "位数为8-20位，需要包含大小写字母和数字 特殊字符（可选）"
        )
        private String password;
        // 当登录失败时，需要返回错误信息
        @Size(min = 4, max = 4, message = "验证码必须为4位")
        private String verificationCode;
    }

    /**
     * 用户注册DTO
     * @author wutong
     */
    @Data
    public static class UserRegisterDTO {
        @NotBlank(message = "用户名不能为空")
        @Size(min = 4, max = 20, message = "用户名长度4-20位")
        @Pattern(
                regexp =USERNAME_REGEXP,
                message = "需以字母开头，可包含数字和下划线，长度4-20位，不能全为数字"
        )
        private String username;
        @NotBlank(message = "密码不能为空")
        @Pattern(
                regexp = PWD_REGEXP,
                message = "位数为8-20位，需要包含大小写字母和数字 特殊字符（可选）"
        )
        private String password;
        @Email(message = "邮箱格式不正确")
        private String email;
        @NotBlank(message = "邮箱验证码不能为空")
        @Size(min = 6, max = 6, message = "邮箱验证码必须为6位")
        private String verificationEmailCode;
        @NotBlank(message = "验证码不能为空")
        @Size(min = 4, max = 4, message = "验证码必须为4位")
        private String verificationCode;
    }

    /**
     * 用户更新信息DTO
     * @author wutong
     */
    @Data
    public static class UserUpdateInfoDTO {
        private Long id;
        private String nickname;
        private User.Sex sex;
        private String phone;
        private String email;
    }

    @Data
    public static class UserUpdatePwdDTO {
        @NotBlank(message = "原密码不能为空")
        @Pattern(
                regexp = PWD_REGEXP,
                message = "位数为8-20位，需要包含大小写字母和数字 特殊字符（可选）"
        )
        private String oldPassword;
        @NotBlank(message = "新密码不能为空")
        @Pattern(
                regexp = PWD_REGEXP,
                message = "位数为8-20位，需要包含大小写字母和数字 特殊字符（可选）"
        )
        private String newPassword;
    }
}
